<template>
  <div id="app">
    <div class="container">
      <div class="header">
        <h1>Redis 客户端连接</h1>
        <p>输入Redis服务器信息建立连接</p>
      </div>

      <div class="form-container">
        <div class="redis-logo">
          <svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
            <path d="M12 0c-4.55 0-8.25 3.7-8.25 8.25v7.5C3.75 20.3 7.45 24 12 24s8.25-3.7 8.25-8.25v-7.5C20.25 3.7 16.55 0 12 0zm0 1.5c3.75 0 6.75 3 6.75 6.75v7.5c0 3.75-3 6.75-6.75 6.75s-6.75-3-6.75-6.75v-7.5C5.25 4.5 8.25 1.5 12 1.5zm0 3c-2.1 0-3.75 1.65-3.75 3.75S9.9 12 12 12s3.75-1.65 3.75-3.75S14.1 4.5 12 4.5zm0 1.5c1.25 0 2.25 1 2.25 2.25S13.25 10.5 12 10.5S9.75 9.5 9.75 8.25S10.75 6 12 6z"/>
          </svg>
        </div>

        <div class="form-group">
          <label for="server-ip">Redis服务器地址</label>
          <input
              type="text"
              id="server-ip"
              v-model="serverIp"
              placeholder="例如: 127.0.0.1 或 redis.example.com"
              @keyup.enter="connectToRedis"
          >
        </div>

        <div class="form-group">
          <label for="server-port">端口号</label>
          <input
              type="number"
              id="server-port"
              v-model.number="serverPort"
              placeholder="默认: 6379"
              min="1"
              max="65535"
              @keyup.enter="connectToRedis"
          >
        </div>

        <div class="form-group">
          <label for="server-password">密码 (可选)</label>
          <input
              type="passwordType"
              id="server-password"
              v-model="password"
              placeholder="输入Redis认证密码"
              @keyup.enter="connectToRedis"
          >
          <span
              class="toggle-password"
              @click="togglePasswordVisibility"
          >{{ passwordVisible ? '🔒' : '👁️' }}</span>
        </div>

        <div class="form-group">
          <label for="connection-alias">连接别名 (自定义)</label>
          <input
              type="text"
              id="connection-alias"
              v-model="alias"
              placeholder="例如: 生产环境Redis"
              @keyup.enter="connectToRedis"
          >
        </div>

        <button
            class="btn"
            @click="connectToRedis"
            :disabled="isConnecting"
        >
          {{ isConnecting ? '连接中...' : '连接到Redis' }}
        </button>

        <div
            class="status"
            :class="{
            success: status.type === 'success',
            error: status.type === 'error'
          }"
            v-if="status.message"
        >
          {{ status.message }}
        </div>
      </div>

      <div class="footer">
        © 2025 Redis客户端 | 高性能键值存储
      </div>
    </div>
  </div>
</template>

<script>
import {configApi} from '@/api/redis'
export default {
  name: 'App',
  data() {
    return {
      serverIp: '',
      serverPort: 6379,
      password: '',
      alias: '',
      passwordVisible: false,
      isConnecting: false,
      status: {
        message: '',
        type: ''
      }
    }
  },
  computed: {
    passwordType() {
      return this.passwordVisible ? 'text' : 'password'
    }
  },
  methods: {
    togglePasswordVisibility() {
      this.passwordVisible = !this.passwordVisible
    },
    connectToRedis() {
      // 验证输入
      if (!this.serverIp) {
        this.showStatus('请输入Redis服务器地址', 'error')
        return
      }

      if (!this.serverPort) {
        this.showStatus('请输入端口号', 'error')
        return
      }

      if (this.serverPort < 1 || this.serverPort > 65535) {
        this.showStatus('端口号必须在1-65535之间', 'error')
        return
      }

      this.isConnecting = true
      //

      this.showStatus(`正在连接 ${this.alias || this.serverIp}:${this.serverPort}...`, 'success')

      // 模拟连接过程
      setTimeout(() => {
        this.isConnecting = false
        this.getClient();
        this.showStatus(`成功连接到Redis服务器 ${this.alias || this.serverIp}:${this.serverPort}`, 'success')
        // 实际应用中这里应该处理Redis连接逻辑
      }, 1500)

    },
    showStatus(message, type) {
      this.status = {
        message,
        type
      }
    },
    getClient() {
      const data = {host: this.serverIp, port: this.serverPort, password: this.password}
      configApi(data).then(res=>{
        console.log("sssssssss", res.data)
        localStorage.setItem('login-status', 'success');
        this.$router.push({path: '/redis2'})
      })
    }
  }
}
</script>

<style scoped>
:root {
  --primary-color: #a41e22;
  --secondary-color: #7a0a0e;
  --accent-color: #d82c32;
  --dark-color: #1a1a1a;
  --light-color: #f8f9fa;
  --error-color: #ff4757;
  --success-color: #2ed573;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

#app {
  //background: url('https://images.unsplash.com/photo-1551269901-5c5e14c25df7?ixlib=rb-1.2.1&auto=format&fit=crop&w=1950&q=80') no-repeat center center fixed;
  background: url('../../assets/bg.jpg') no-repeat center center fixed;
  background-size: cover;
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
  position: relative;
}

#app::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.6);
  z-index: 0;
}

.container {
  width: 100%;
  max-width: 500px;
  background-color: rgba(255, 255, 255, 0.95);
  border-radius: 16px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
  overflow: hidden;
  animation: fadeIn 0.5s ease-out;
  position: relative;
  z-index: 1;
  backdrop-filter: blur(5px);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(20px); }
  to { opacity: 1; transform: translateY(0); }
}

.header {
  background: linear-gradient(to right, #a92e30, #7a0a0e);
  color: white;
  padding: 24px;
  text-align: center;
  position: relative;
  overflow: hidden;
}

.header::before {
  content: '';
  position: absolute;
  top: -10px;
  left: -10px;
  right: -10px;
  bottom: -10px;
  background: linear-gradient(45deg,
  rgba(255,255,255,0.1) 0%,
  rgba(255,255,255,0) 20%,
  rgba(255,255,255,0) 80%,
  rgba(255,255,255,0.1) 100%);
  z-index: 1;
}

.header h1 {
  font-size: 1.8rem;
  margin-bottom: 8px;
  font-weight: 600;
  position: relative;
  z-index: 2;
}

.header p {
  opacity: 0.9;
  font-size: 0.9rem;
  position: relative;
  z-index: 2;
}

.form-container {
  padding: 32px;
}

.form-group {
  margin-bottom: 24px;
  position: relative;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  font-weight: 500;
  color: var(--dark-color);
  font-size: 0.95rem;
}

.form-group input {
  width: 100%;
  padding: 14px 16px;
  border: 2px solid #e9ecef;
  border-radius: 8px;
  font-size: 1rem;
  transition: all 0.3s ease;
  background-color: rgba(255, 255, 255, 0.8);
}

.form-group input:focus {
  outline: none;
  border-color: var(--accent-color);
  background-color: white;
  box-shadow: 0 0 0 3px rgba(168, 30, 34, 0.2);
}

.form-group input::placeholder {
  color: #adb5bd;
}

.toggle-password {
  position: absolute;
  right: 15px;
  top: 40px;
  cursor: pointer;
  color: #adb5bd;
  font-size: 1.2rem;
  transition: all 0.3s ease;
  user-select: none;
}

.toggle-password:hover {
  color: var(--accent-color);
}

.btn {
  display: inline-block;
  background: linear-gradient(to right, #a92e31, #831c1f);
  color: white;
  border: none;
  padding: 14px 24px;
  font-size: 1rem;
  font-weight: 500;
  border-radius: 8px;
  cursor: pointer;
  width: 100%;
  transition: all 0.3s ease;
  text-transform: uppercase;
  letter-spacing: 1px;
  position: relative;
  overflow: hidden;
}

.btn::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg,
  transparent,
  rgba(255, 255, 255, 0.2),
  transparent);
  transition: all 0.5s ease;
}

.btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(168, 30, 34, 0.4);
}

.btn:hover::before {
  left: 100%;
}

.btn:active {
  transform: translateY(0);
}

.btn:disabled {
  opacity: 0.7;
  cursor: not-allowed;
  transform: none !important;
  box-shadow: none !important;
}

.btn:disabled::before {
  display: none;
}

.status {
  margin-top: 20px;
  padding: 12px;
  border-radius: 8px;
  text-align: center;
  font-size: 0.9rem;
}

.status.success {
  background-color: rgba(46, 213, 115, 0.2);
  color: var(--success-color);
}

.status.error {
  background-color: rgba(255, 71, 87, 0.2);
  color: var(--error-color);
}

.footer {
  text-align: center;
  padding: 16px;
  color: #6c757d;
  font-size: 0.85rem;
  border-top: 1px solid rgba(0, 0, 0, 0.1);
}

.redis-logo {
  text-align: center;
  margin-bottom: 20px;
}

.redis-logo svg {
  width: 60px;
  height: 60px;
  fill: var(--primary-color);
}

@media (max-width: 576px) {
  .container {
    border-radius: 12px;
  }

  .header {
    padding: 20px;
  }

  .form-container {
    padding: 24px;
  }
}
</style>